package cn.com.generaldata.os.service.indicator_evaluation.coefficient_of_variation;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 区域协调性综合评价指数评估指标计算类.
 */
public class RegionBalancedIndexCalculateService extends IndicatorEvaluationCalculateService {

	/** 变异默认权重值 **/
	private Map<String, Double> defaultVarCoefficientWeight = new HashMap<String, Double>() {
		{
			put("人均GDP的变异系数", 0.1000795);
			put("区位熵变异系数", 0.1000795);
			put("GOP变异系数", 0.190641699);
			put("人均GOP变异系数", 0.106264611);
			put("海洋经济增长率变异系数", 0.063334691);
			put("三次产业结构变动度变异系数", 0.280081);
			put("港口货物吞吐量变异系数", 0.119893);
			put("GOP的Moran's I系数", 0.039679);
		}
	};

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 先保留
		}
		return result;

	}

	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		double sum = 0d;
		for (String indicatorName : defaultVarCoefficientWeight.keySet()) {
			double varCoefficient = getVarCoefficient(indicatorAccountVO, indicatorName);
			double varVarCoefficientWeight = getVarCoefficientWeight(indicatorAccountVO, indicatorName);
			sum += varCoefficient * varVarCoefficientWeight;
		}

		return 1 / sum;
	}

	/**
	 * 获得各项变异系数.
	 * 
	 * @param indicatorAccountVO
	 * @param indicatorName
	 * @return
	 */
	private Double getVarCoefficient(IndicatorAccountVO indicatorAccountVO, String indicatorName) {
		Integer indicatorId = getIndicatorEvaluationId(indicatorName, indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorId);
		Double result = getEvaluationIndicatorValue(cri);
		Validate.isTrue(result != null);
		return result;
	}

	/**
	 * 获得各项变异系数权重值.
	 * 
	 * @param indicatorAccountVO
	 * @param varCoefficientIndicatorName
	 * @return
	 */
	private Double getVarCoefficientWeight(IndicatorAccountVO indicatorAccountVO, String varCoefficientIndicatorName) {
		String varCoefficientWeightIndicatorName = varCoefficientIndicatorName + "权重值";
		Integer indicatorId = getIndicatorInputId(varCoefficientWeightIndicatorName, null);
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorId);
		Double result = getInputIndicatorValue(cri);
		return result == null ? defaultVarCoefficientWeight.get(varCoefficientIndicatorName) : result;
	}

}
